﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DALFactory;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class PodBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IPodDA PodDA = DataAccess.CreatePodDA();
        private static readonly IPodDetailDA PodDetailDA = DataAccess.CreatePodDetailDA();
        private PubParasInfo pubParasInfo;

        public PodBLL()
        {
            this.pubParasInfo = null;
        }

        public PodBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public PodBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string ConFirm(PodInfo podInfo)
        {
            string podID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                PodDA.UpdatePod(dataBase, tran, podInfo, this.dataBaseID);
                podID = podInfo.PodID;
                if ((podInfo.PodDetailInfos != null) && (podInfo.PodDetailInfos.Count > 0))
                {
                    for (i = 0; i < podInfo.PodDetailInfos.Count; i++)
                    {
                        PodDetailDA.UpdatePodDetail(dataBase, tran, podInfo.PodDetailInfos[i], this.dataBaseID);
                    }
                }
                IDeliveryOrderDA DeliveryOrderDA = DataAccess.CreateDeliveryOrderDA();
                DeliveryOrderInfo deliveryOrderInfo = DeliveryOrderDA.GetDeliveryOrderByID(dataBase, tran, podInfo.DeliveryID, this.dataBaseID[0], this.pubParasInfo);
                if (deliveryOrderInfo != null)
                {
                    deliveryOrderInfo.Status = DeliveryOrderStatus.POD;
                    DeliveryOrderDA.UpdateDeliveryOrder(dataBase, tran, deliveryOrderInfo, this.dataBaseID);
                    IDeliveryOrderDetailDA DeliveryOrderDetailDA = DataAccess.CreateDeliveryOrderDetailDA();
                    List<DeliveryOrderDetailInfo> deliveryOrderDetailInfos = DeliveryOrderDetailDA.GetDeliveryOrderDetailListByID(dataBase, tran, podInfo.DeliveryID, this.dataBaseID[0], this.pubParasInfo);
                    if ((deliveryOrderDetailInfos != null) && (deliveryOrderDetailInfos.Count > 0))
                    {
                        for (i = 0; i < deliveryOrderDetailInfos.Count; i++)
                        {
                            if (deliveryOrderDetailInfos[i] != null)
                            {
                                deliveryOrderDetailInfos[i].Status = DeliveryOrderStatus.POD;
                                DeliveryOrderDetailDA.UpdateDeliveryOrderDetail(dataBase, tran, deliveryOrderDetailInfos[i], this.dataBaseID);
                            }
                        }
                    }
                }
                IDriverDA DriverDA = DataAccess.CreateDriverDA();
                DriverInfo driverInfo = DriverDA.GetDriverByID(dataBase, tran, podInfo.DriverID, this.dataBaseID[0], this.pubParasInfo);
                if (driverInfo != null)
                {
                    driverInfo.Status = DriverStatus.FREE;
                    DriverDA.UpdateDriver(dataBase, tran, driverInfo, this.dataBaseID);
                }
                ITransportEquipmentDA TransportEquipmentDA = DataAccess.CreateTransportEquipmentDA();
                TransportEquipmentInfo transportEquipmentInfo = TransportEquipmentDA.GetTransportEquipmentByID(dataBase, tran, podInfo.EquipmentID, this.dataBaseID[0], this.pubParasInfo);
                if (transportEquipmentInfo != null)
                {
                    transportEquipmentInfo.Status = TransportEquipmentStatus.FREE;
                    TransportEquipmentDA.UpdateTransportEquipment(dataBase, tran, transportEquipmentInfo, this.dataBaseID);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return podID;
        }

        public string Delete(List<string> podInfoIDs)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<PodInfo> podInfos = new List<PodInfo>();
                PodInfo podInfo = null;
                foreach (string id in podInfoIDs)
                {
                    podInfo = PodDA.GetPodByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    podInfo.PodDetailInfos = PodDetailDA.GetPodDetailListByID(dataBase, tran, id, this.dataBaseID[0]);
                    podInfos.Add(podInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                PodDetailDA.DeletePodDetail(dataBase, tran, podInfoIDs, this.dataBaseID);
                PodDA.DeletePod(dataBase, tran, podInfoIDs, this.dataBaseID);
                for (int i = 0; i < podInfoIDs.Count; i++)
                {
                    auditDataBLL.InsertAuditData<PodInfo>(dataBase, tran, podInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "POD", this.dataBaseID[0]);
                    if ((podInfos[i].PodDetailInfos != null) && (podInfos[i].PodDetailInfos.Count > 0))
                    {
                        for (int j = 0; j < podInfos[i].PodDetailInfos.Count; j++)
                        {
                            auditDataBLL.InsertAuditData<PodDetailInfo>(dataBase, tran, podInfos[i].PodDetailInfos[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "POD_DETAIL", this.dataBaseID[0]);
                        }
                    }
                    temp = temp + podInfoIDs[i] + ",";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllInfo()
        {
            return PodDA.GetAllPod(this.dataBaseID[0], this.pubParasInfo);
        }

        public PodInfo GetInfoByID(string podID)
        {
            PodInfo podInfo = PodDA.GetPodByID(podID, this.dataBaseID[0], this.pubParasInfo);
            if (podInfo != null)
            {
                podInfo.PodDetailInfos = PodDetailDA.GetPodDetailListByID(podInfo.PodID, this.dataBaseID[0]);
            }
            return podInfo;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            PodQueryEntity podQuery = obj as PodQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(podQuery.DeliveryID))
            {
                sqlWhere.Add(" POD.DELIVERY_ID " + podQuery.DeliveryID);
            }
            if (!string.IsNullOrEmpty(podQuery.PodID))
            {
                sqlWhere.Add(" POD.POD_ID " + podQuery.PodID);
            }
            if (!string.IsNullOrEmpty(podQuery.CarrierID))
            {
                sqlWhere.Add(" POD.CARRIER_ID " + podQuery.CarrierID);
            }
            if (!string.IsNullOrEmpty(podQuery.CreatedBy))
            {
                sqlWhere.Add(" POD.CREATED_BY " + podQuery.CreatedBy);
            }
            if (!string.IsNullOrEmpty(podQuery.BolID))
            {
                sqlWhere.Add(" POD.BOL_ID " + podQuery.BolID);
            }
            if (!string.IsNullOrEmpty(podQuery.SignedBy))
            {
                sqlWhere.Add(" POD.SIGNED_BY " + podQuery.SignedBy);
            }
            if (!string.IsNullOrEmpty(podQuery.Status))
            {
                sqlWhere.Add(" POD.STATUS " + podQuery.Status);
            }
            if (!string.IsNullOrEmpty(podQuery.SignedDate))
            {
                sqlWhere.Add(podQuery.SignedDate.Replace("@SignDate@", "POD.SIGNED_DATE"));
            }
            return PodDA.GetPodByQueryList(sqlWhere, podQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string Insert(object obj)
        {
            string podID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                PodInfo podInfo = obj as PodInfo;
                if (!string.IsNullOrEmpty(podInfo.PodID))
                {
                    if (PodDA.CheckPodIDUnique(dataBase, tran, podInfo.PodID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F8500_001", this.pubParasInfo.UserLanguage, new List<string> { podInfo.PodID });
                    }
                }
                else
                {
                    podInfo.PodID = commonMethod.GenNextNumStr(dataBase, tran, "POD", this.dataBaseID[0]);
                }
                podInfo.UpdatedBy = podInfo.CreatedBy;
                podInfo.CreatedDate = DateTime.Now;
                podInfo.UpdatedDate = DateTime.Now;
                PodDA.InsertPod(dataBase, tran, podInfo, this.dataBaseID);
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                auditDataBLL.InsertAuditData<PodInfo>(dataBase, tran, null, podInfo, AuditAction.Insert, this.pubParasInfo.UserID, "POD", this.dataBaseID[0]);
                podID = podInfo.PodID;
                if ((podInfo.PodDetailInfos != null) && (podInfo.PodDetailInfos.Count > 0))
                {
                    for (int i = 0; i < podInfo.PodDetailInfos.Count; i++)
                    {
                        if (PodDetailDA.CheckPodDetailIDUnique(dataBase, tran, podInfo.PodDetailInfos[i].PodID, podInfo.PodDetailInfos[i].LineID.ToString(), this.dataBaseID[0]))
                        {
                            throw new Exception(string.Concat(new object[] { "PodID:", podInfo.PodDetailInfos[i].PodID, "',LineID:", podInfo.PodDetailInfos[i].LineID, "'已经存在! 请重新输入!" }));
                        }
                        podInfo.PodDetailInfos[i].PodID = podID;
                        podInfo.PodDetailInfos[i].CreatedBy = podInfo.CreatedBy;
                        podInfo.PodDetailInfos[i].UpdatedBy = podInfo.UpdatedBy;
                        podInfo.PodDetailInfos[i].CreatedDate = DateTime.Now;
                        podInfo.PodDetailInfos[i].UpdatedDate = DateTime.Now;
                        PodDetailDA.InsertPodDetail(dataBase, tran, podInfo.PodDetailInfos[i], this.dataBaseID);
                        auditDataBLL.InsertAuditData<PodDetailInfo>(dataBase, tran, null, podInfo.PodDetailInfos[i], AuditAction.Insert, this.pubParasInfo.UserID, "POD_DETAIL", this.dataBaseID[0]);
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return podID;
        }

        public string Update(object obj)
        {
            string podID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                PodInfo podInfo = obj as PodInfo;
                podInfo.UpdatedDate = DateTime.Now;
                PodInfo oldPodInfo = PodDA.GetPodByID(dataBase, tran, podInfo.PodID, this.dataBaseID[0], this.pubParasInfo);
                if (PodDA.UpdatePod(podInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<PodInfo>(oldPodInfo, podInfo, AuditAction.Update, this.pubParasInfo.UserID, "POD", this.dataBaseID[0]);
                    podID = podInfo.PodID;
                }
                if ((podInfo.PodDetailInfos != null) && (podInfo.PodDetailInfos.Count > 0))
                {
                    for (i = 0; i < podInfo.PodDetailInfos.Count; i++)
                    {
                        PodDetailDA.UpdatePodDetail(dataBase, tran, podInfo.PodDetailInfos[i], this.dataBaseID);
                    }
                }
                IDeliveryOrderDA DeliveryOrderDA = DataAccess.CreateDeliveryOrderDA();
                DeliveryOrderInfo deliveryOrderInfo = DeliveryOrderDA.GetDeliveryOrderByID(dataBase, tran, podInfo.DeliveryID, this.dataBaseID[0], this.pubParasInfo);
                if (deliveryOrderInfo != null)
                {
                    deliveryOrderInfo.Status = DeliveryOrderStatus.SIGNOFF;
                    DeliveryOrderDA.UpdateDeliveryOrder(dataBase, tran, deliveryOrderInfo, this.dataBaseID);
                    IDeliveryOrderDetailDA DeliveryOrderDetailDA = DataAccess.CreateDeliveryOrderDetailDA();
                    List<DeliveryOrderDetailInfo> deliveryOrderDetailInfos = DeliveryOrderDetailDA.GetDeliveryOrderDetailListByID(dataBase, tran, podInfo.DeliveryID, this.dataBaseID[0], this.pubParasInfo);
                    if ((deliveryOrderDetailInfos != null) && (deliveryOrderDetailInfos.Count > 0))
                    {
                        for (i = 0; i < deliveryOrderDetailInfos.Count; i++)
                        {
                            if (deliveryOrderDetailInfos[i] != null)
                            {
                                deliveryOrderDetailInfos[i].Status = DeliveryOrderStatus.SIGNOFF;
                                DeliveryOrderDetailDA.UpdateDeliveryOrderDetail(dataBase, tran, deliveryOrderDetailInfos[i], this.dataBaseID);
                            }
                        }
                    }
                }
                IDriverDA DriverDA = DataAccess.CreateDriverDA();
                DriverInfo driverInfo = DriverDA.GetDriverByID(dataBase, tran, podInfo.DriverID, this.dataBaseID[0], this.pubParasInfo);
                if (driverInfo != null)
                {
                    driverInfo.Status = DriverStatus.BACKHAUL;
                    DriverDA.UpdateDriver(dataBase, tran, driverInfo, this.dataBaseID);
                }
                ITransportEquipmentDA TransportEquipmentDA = DataAccess.CreateTransportEquipmentDA();
                TransportEquipmentInfo transportEquipmentInfo = TransportEquipmentDA.GetTransportEquipmentByID(dataBase, tran, podInfo.EquipmentID, this.dataBaseID[0], this.pubParasInfo);
                if (transportEquipmentInfo != null)
                {
                    transportEquipmentInfo.Status = TransportEquipmentStatus.BACKHAUL;
                    TransportEquipmentDA.UpdateTransportEquipment(dataBase, tran, transportEquipmentInfo, this.dataBaseID);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return podID;
        }
    }
}

